package com.mediacloud.app.newsmodule.view;

import android.content.Context;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import androidx.appcompat.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.mediacloud.app.newsmodule.R;

/**
 * Created by hello-world on 2018/4/16.
 */

public class CommentBadgetTextView extends AppCompatTextView {
    public CommentBadgetTextView(Context context) {
        super(context);
    }

    public CommentBadgetTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CommentBadgetTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void setBackground(int badgeColor)
    {
        float radius = getContext().getResources().getDimensionPixelOffset(R.dimen.dimen8);
        // 前2个 左上角， 3 4 ， 右上角， 56， 右下， 78 ，左下
        float radiusArray[] = {
                radius, radius, radius, radius, radius, radius, radius, radius
        };
        RoundRectShape roundRect = new RoundRectShape(radiusArray, null, null);
        ShapeDrawable bgDrawable = new ShapeDrawable(roundRect);
        bgDrawable.getPaint().setColor(badgeColor);
        setBackgroundDrawable(bgDrawable);
        if("0".equals(getText())|| TextUtils.isEmpty(getText()))
            setVisibility(GONE);
        else
            setVisibility(VISIBLE);
    }
    private int dip2Px(float dip)
    {
        return (int)(dip * getContext().getResources().getDisplayMetrics().density + 0.5F);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        if("0".equals(getText())|| TextUtils.isEmpty(getText()))
            setVisibility(GONE);
        else
            setVisibility(VISIBLE);
    }
}